package Time

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionTime struct {
	transaction.TransactionBase
	user *user.User
	req  *codecs.Message
	resp *cs_message.TimeToC
}

func (this *transactionTime) GetModuleName() string {
	return "Time"
}

func (this *transactionTime) GetReplyMsg() proto.Message {
	return nil
}

func (this *transactionTime) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionTime) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionTime) Begin() {
	this.resp = &cs_message.TimeToC{
		ServerTime: proto.Int64(time.Now().Unix()),
	}
	this.user.EndTrans(this)
}

func (this *transactionTime) End() {
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionTime) Timeout() {
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.TimeToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionTime{
			user: user,
			req:  msg,
		}
	})
}
